package com.itheima.反射.反射获取类中方法;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author 王加鑫
 * @Date 2021/5/26 16:16
 * @Version 1.0
 */
public class Demo {
    public static void main(String[] args) {
        //method1();
        //method2();
        method3();


    }

    private static void method3() {
        try {
            Class<Student1> student1Class = Student1.class;
            Method getSum = student1Class.getMethod("getSum", int.class, int.class);
            Student1 student = student1Class.newInstance();
            Object result = getSum.invoke(student, 10, 20);
            System.out.println(result);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private static void method2() {
        Class<Student1> student1Class = Student1.class;
        Method[] declaredMethods = student1Class.getDeclaredMethods();
        for (Method method : declaredMethods) {
            System.out.println(method);
        }
    }

    private static void method1() {
        Class<Student1> student1Class = Student1.class;
        Method[] methods = student1Class.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
    }
}
